package com.base.vistter.iframe.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.base.vistter.iframe.entity.InverterAlarmError;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.mapper.InverterAlarmErrorMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Slf4j
public class InverterAlarmErrorService extends BaseService<InverterAlarmErrorMapper, InverterAlarmError>{

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean save(InverterAlarmError entity) throws IframeException {
        LambdaQueryWrapper<InverterAlarmError> lambdaQueryWrapper = new LambdaQueryWrapper<InverterAlarmError>();
        lambdaQueryWrapper.eq(InverterAlarmError::getCode, entity.getCode());
        if (super.selectCount(lambdaQueryWrapper) > 0l) {
            throw new IframeException("告警编码已存在");
        }
        return super.save(entity);
    }

    @Transactional(readOnly = true)
    public InverterAlarmError getInverterAlarmErrorByCode(String code) throws IframeException {
        LambdaQueryWrapper<InverterAlarmError> lambdaQueryWrapper = new LambdaQueryWrapper<InverterAlarmError>();
        lambdaQueryWrapper.eq(InverterAlarmError::getCode, code);
        return super.selectOne(lambdaQueryWrapper);
    }

}
